package com.cdm.beans;

import net.sf.frails.JSFHelper;
import net.sf.frails.bean.annotations.DefMbean;
import net.sf.frails.bean.annotations.PreRender;
import net.sf.frails.bean.annotations.Prop;

import com.cdm.domain.service.Service;
import com.cdm.domain.service.ServiceImpl;
import com.cdm.persistence.Sequence;
import com.cdm.persistence.User;
import com.cdm.util.Constants;

@DefMbean
public class HomeBean {

	@Prop
	String loginName;
	@Prop
	String password;

	@Prop
	boolean create = true;

	@PreRender(failOutcome = "home")
	public boolean init() {
		System.out.println("Enter Home Page");
		return true;
	}

	public String gotoUserHome() {
		System.out.println("Go To UserHomePage from HomePage");
		// return "userHome";
		return "";
	}

	public String login() {
		User login = service.login(loginName, password, "");
		if (login != null) {
			if (login.getAuthStatus() == User.UNAUTH) {
				JSFHelper.addMessage("error_user_not_authed");
				return "/home";
			}
			JSFHelper.putIntoApplication(Constants.LOGIN_USER_KEY, login);
			JSFHelper.addMessage("message_login_success");

			Integer role = login.getUserRole();
			switch (role) {
			case User.ROLE_CP:
				return "r:/user/cp";
			case User.ROLE_SP:
				return "r:/user/sp";
			case User.ROLE_CDM:
				return "r:/user/cdm";
			case User.ROLE_ADMIN:
				return "r:/user/admin";
			case User.ROLE_END_USER:
				return "r:/user/endUser";
			default:
				return "r:/user/endUser";
			}

		}
		JSFHelper.addMessage("message_login_failed");
		return "/home";
	}

	private Service service = new ServiceImpl();

}
